package com.tsingteng.cosfun.ui.message.comment;

import android.content.Intent;
import android.os.Bundle;
import android.provider.FontsContract;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.CenterThreeNotification;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.ui.cosfun.PlayActivity;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.utils.TimeUtils;
import com.tsingteng.cosfun.utils.ToastUtils;

import java.text.ParseException;
import java.util.Date;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/7
 * qq(872179966)
 */

public class OtherAdapter extends BaseQuickAdapter<CenterThreeNotification.ListBean, BaseViewHolder> {

    private OtherContract.OtherClickListener otherClickListener;

    public OtherAdapter() {
        super(R.layout.item_other_adapter);
    }


    @Override
    protected void convert(BaseViewHolder helper, CenterThreeNotification.ListBean item) {
        helper.setText(R.id.tv_other_title, item.getSource().getNickName() + "");
        CircleImageView view = helper.getView(R.id.iv_other_header);
        ImageUtils.LoadImage(mContext, item.getSource().getImage()+Config.YA_SUO_IMAGE_URL, view,R.mipmap.ic_launcher);
        ImageView iv_other_image = helper.getView(R.id.iv_other_image);
        TextView tv_other_content = helper.getView(R.id.tv_other_content);
        long createTime = item.getCreateTime();
        boolean isYesterday = false;
        try {
            isYesterday = TimeUtils.IsYesterday(createTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (isYesterday) {
            helper.setText(R.id.tv_other_time, "昨天");
        }else {
            helper.setText(R.id.tv_other_time, TimeUtils.formatChatTime(new Date(createTime)));
        }

        TextView tv_other_why = helper.getView(R.id.tv_other_why);
        String workType = item.getWorkType();
        String relatedContent = item.getRelatedContent();//回复的类容
        int relatedId = item.getRelatedId();//类型id

        String relatedType = item.getRelatedType();//是什么
        if (TextUtils.equals("MERGE", relatedType)) {
            helper.setText(R.id.tv_other_title, item.getRelatedContent());
        }
        if (TextUtils.equals(item.getType(), "FAVOR")) {//赞我
            if (TextUtils.equals("VIDEO", workType)) {
                if (TextUtils.isEmpty(relatedType)) {
                    tv_other_why.setText("点赞了你的作品");
                } else {
                    tv_other_why.setText("点赞了你的评论");
                }
                ImageUtils.LoadImage(mContext, item.getVideoCverUrl(), iv_other_image,R.mipmap.ic_launcher);
                iv_other_image.setVisibility(View.VISIBLE);
                tv_other_content.setVisibility(View.GONE);
            } else if (TextUtils.equals("PARAS", workType)) {
                if (TextUtils.isEmpty(relatedType)) {
                    tv_other_why.setText("点赞了你的盒饭");
                } else {
                    tv_other_why.setText("点赞了你的评论");
                }
                tv_other_content.setText(item.getWorkContent());
                iv_other_image.setVisibility(View.GONE);
                tv_other_content.setVisibility(View.VISIBLE);
            }
        } else if (TextUtils.equals(item.getType(), "REVIEW")) {        //评论我
            if (TextUtils.equals("VIDEO", workType)) {
                if (TextUtils.equals("REVIEW", relatedType)) {
                    tv_other_why.setText("评论了你的作品");
                } else if (TextUtils.equals("REPLY", relatedType)) {
                    tv_other_why.setText("回复了你的评论");
                } else if (TextUtils.equals("AUTHOR_REPLY", relatedType)) {
                    tv_other_why.setText("在TA的作品中回复了 你");
                } else {
                    tv_other_why.setText("评论了你的作品");
                }
                ImageUtils.LoadImage(mContext, item.getVideoCverUrl(), iv_other_image,R.mipmap.ic_launcher);
                iv_other_image.setVisibility(View.VISIBLE);
                tv_other_content.setVisibility(View.GONE);
            } else if (TextUtils.equals("PARAS", workType)) {
                if (TextUtils.equals("REVIEW", relatedType)) {
                    tv_other_why.setText("评论了你的作品");
                } else if (TextUtils.equals("REPLY", relatedType)) {
                    tv_other_why.setText("回复了你的评论");
                } else if (TextUtils.equals("AUTHOR_REPLY", relatedType)) {
                    tv_other_why.setText("在TA的作品中回复了 你");
                } else {
                    tv_other_why.setText("评论了你的作品");
                }

                tv_other_content.setText(item.getWorkContent());
                iv_other_image.setVisibility(View.GONE);
                tv_other_content.setVisibility(View.VISIBLE);
            }

        } else if (TextUtils.equals(item.getType(), "POINT")) {//@我
            if (TextUtils.equals("VIDEO", workType)) {
                ImageUtils.LoadImage(mContext, item.getVideoCverUrl(), iv_other_image,R.mipmap.ic_launcher);
                iv_other_image.setVisibility(View.VISIBLE);
                tv_other_content.setVisibility(View.GONE);
            } else if (TextUtils.equals("PARAS", workType)) {
                tv_other_content.setText(item.getWorkContent());
                iv_other_image.setVisibility(View.GONE);
                tv_other_content.setVisibility(View.VISIBLE);
            }

            tv_other_why.setText(" 在 TA 的评论中@了你");
        }
        //设置右上方红点
        ImageView iv_other_isread = helper.getView(R.id.iv_other_isread);
        int state = item.getState();
        if (state == 0) {
            iv_other_isread.setVisibility(View.VISIBLE);
        } else

        {
            iv_other_isread.setVisibility(View.INVISIBLE);
        }
        //点击头像跳转用户主页
        helper.getView(R.id.iv_other_header).setOnClickListener(view1 -> {
//                Navigate.startUserHome(mContext,tag);
            Bundle bundle = new Bundle();
            bundle.putInt("user_id", item.getSource().getId());
            bundle.putString("worjId", item.getWorkId() + "");
            bundle.putInt(Constant.TAG, 1);
            TerminalActivity.showFragment(mContext, HomeMineFragment.class, bundle);
        });

        //点击跳转段子详情页
        tv_other_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                otherClickListener.otherClick(item);
//                int relatedId = item.getWorkId();
//                Navigate.startPlayDetails(mContext, relatedId, item.getSource().getNickName(), item.getSource().getId(), 0);
            }
        });
        //跳转视屏
        iv_other_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PlayActivity.class);
                intent.putExtra("type", 3);
                intent.putExtra("id", item.getWorkId());
                intent.getIntExtra("position", 0);
                mContext.startActivity(intent);
            }
        });
    }

    public void setOtherClickListener(OtherContract.OtherClickListener otherClickListener) {
        this.otherClickListener = otherClickListener;
    }
}
